//
//  YDRealNameAuthController.m
//  siyanone
//
//  Created by LK-IOS on 2018/3/28.
//  Copyright © 2018年 LK-News. All rights reserved.
//

#import "YDRealNameAuthController.h"


@interface YDRealNameAuthController ()<UITextFieldDelegate>
//提交按钮
@property (nonatomic , weak) SYNoHeightedButton *sendButton;
//姓名
@property (nonatomic , weak) UILabel *nameLabel;
//姓名输入框
@property (nonatomic , weak) UITextField *nameTextField;
//身份证
@property (nonatomic , weak) UILabel *cardLabel;
//身份证输入框
@property (nonatomic , weak) UITextField *cardTextField;



//姓名
@property (nonatomic , copy) NSString *nameString;
//身份证号
@property (nonatomic , copy) NSString *cardString;

@end

@implementation YDRealNameAuthController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:mainGray];
    self.title = @"实名认证";
    [self addChildControls];
    
    
    [self.nameTextField addTarget:self action:@selector(textchange) forControlEvents:UIControlEventAllEditingEvents];
    [self.cardTextField addTarget:self action:@selector(textchange) forControlEvents:UIControlEventAllEditingEvents];
}


-(void)addChildControls
{
    [self nameTextField];
    [self cardTextField];
    [self nameLabel];
    [self cardLabel];
    [self sendButton];
}

-(void)textchange
{
    self.sendButton.enabled = self.nameTextField.text.length && self.cardTextField.text.length;
}

-(UITextField *)nameTextField
{
    if (_nameTextField == nil) {
        UITextField *nameTextField = [[UITextField alloc] init];
        [self.view addSubview:nameTextField];
        nameTextField.textColor = [UIColor blackColor];
        nameTextField.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        nameTextField.placeholder = @"请输入姓名";
        nameTextField.backgroundColor = [UIColor clearColor];
        nameTextField.borderStyle = UITextBorderStyleNone;
        nameTextField.textAlignment = NSTextAlignmentLeft;
        nameTextField.keyboardType = UIKeyboardTypeDefault;
        nameTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        nameTextField.keyboardAppearance=UIKeyboardAppearanceDefault;
        nameTextField.returnKeyType =UIReturnKeyDone;
        [nameTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
        [nameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(rate375(277)));
            if (isPhoneX) {
                make.top.equalTo(self.view).offset(rateX812(20));
                make.height.equalTo(@(rateX812(32)));
            }else{
                make.top.equalTo(self.view).offset(rate750(20));
                make.height.equalTo(@(rate750(32)));
            }
            make.right.equalTo(self.view).offset(-rate375(20));
        }];
        nameTextField.tintColor = [UIColor blackColor];
        nameTextField.layer.borderColor = [UIColor colorWithHexString:@"DEE0E4"].CGColor;
        nameTextField.layer.borderWidth = 1;
        nameTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
        nameTextField.leftViewMode = UITextFieldViewModeAlways;
        nameTextField.delegate = self;
        _nameTextField = nameTextField;
    }
    return _nameTextField;
}


-(UITextField *)cardTextField
{
    if (_cardTextField == nil) {
        UITextField *cardTextField = [[UITextField alloc] init];
        [self.view addSubview:cardTextField];
        cardTextField.textColor = [UIColor blackColor];
        cardTextField.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        cardTextField.placeholder = @"请输入身份证号码";
        cardTextField.backgroundColor = [UIColor clearColor];
        cardTextField.borderStyle = UITextBorderStyleNone;
        cardTextField.textAlignment = NSTextAlignmentLeft;
        cardTextField.keyboardType = UIKeyboardTypeDefault;
        cardTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        cardTextField.keyboardAppearance=UIKeyboardAppearanceDefault;
        cardTextField.returnKeyType =UIReturnKeyDone;
        [cardTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
        [cardTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(rate375(277)));
            if (isPhoneX) {
                make.top.equalTo(self.nameTextField.mas_bottom).offset(rateX812(20));
                make.height.equalTo(@(rateX812(32)));
            }else{
                make.top.equalTo(self.nameTextField.mas_bottom).offset(rate750(20));
                make.height.equalTo(@(rate750(32)));
            }
            make.right.equalTo(self.view).offset(-rate375(20));
        }];
        cardTextField.tintColor = [UIColor blackColor];
        cardTextField.layer.borderColor = [UIColor colorWithHexString:@"DEE0E4"].CGColor;
        cardTextField.layer.borderWidth = 1;
        cardTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
        cardTextField.leftViewMode = UITextFieldViewModeAlways;
        cardTextField.delegate = self;
        _cardTextField = cardTextField;
    }
    return _cardTextField;
}

-(UILabel *)nameLabel
{
    if (_nameLabel == nil) {
        UILabel *nameLabel = [[UILabel alloc] init];
        [self.view addSubview:nameLabel];
        nameLabel.textColor = [UIColor colorWithHexString:@"687284"];
        nameLabel.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        nameLabel.textAlignment = NSTextAlignmentLeft;
        nameLabel.text = @"姓名";
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.nameTextField);
            make.left.equalTo(self.view).offset(rate375(20));
        }];
        _nameLabel = nameLabel;
    }
    return _nameLabel;
}
-(UILabel *)cardLabel
{
    if (_cardLabel == nil) {
        UILabel *cardLabel = [[UILabel alloc] init];
        [self.view addSubview:cardLabel];
        cardLabel.textColor = [UIColor colorWithHexString:@"687284"];
        cardLabel.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        cardLabel.textAlignment = NSTextAlignmentLeft;
        cardLabel.text = @"身份证";
        [cardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.cardTextField);
            make.left.equalTo(self.view).offset(rate375(20));
        }];
        _cardLabel = cardLabel;
    }
    return _cardLabel;
}


-(SYNoHeightedButton *)sendButton
{
    if (_sendButton == nil) {
        SYNoHeightedButton *sendButton = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        [self.view addSubview:sendButton];
        [sendButton setBackgroundImage:[UIImage imageWithColor:zhidianAlprColor(0.3)] forState:UIControlStateDisabled];
        [sendButton setBackgroundImage:[UIImage imageWithColor:zhidainMainColor] forState:UIControlStateNormal];
        [sendButton setTitle:@"提交" forState:UIControlStateNormal];
        [sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
        sendButton.titleLabel.font = [UIFont systemFontOfSize:font16ScreenWRate * LKScreenW];
        sendButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.equalTo(self.view);
            make.width.equalTo(@(rate375(277)));
            if (isPhoneX) {
                make.height.equalTo(@(rateX812(48)));
                make.top.equalTo(self.cardLabel.mas_bottom).offset(rateX812(68));
            }else{
                make.height.equalTo(@(rate750(48)));
                make.top.equalTo(self.cardLabel.mas_bottom).offset(rate750(68));
            }
        }];
        sendButton.enabled = NO;
        [sendButton addTarget:self action:@selector(sendButtonClick:) forControlEvents:UIControlEventTouchDown];
        sendButton.layer.cornerRadius = 5.0f;
        sendButton.layer.masksToBounds = YES;
        
        _sendButton = sendButton;
    }
    return _sendButton;
}

-(void)sendButtonClick:(SYNoHeightedButton *)button
{
    
    [self.view endEditing:YES];
    self.nameString = self.nameTextField.text;
    self.cardString = self.cardTextField.text;
    
    
    if (!self.nameString) {
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view
                                                        WithType:NavTipsViewTypeMessage
                                                            text:@"请输入姓名"
                                                      isExistNav:YES];
        return;
    }else if (![NSString checkUserIdCard:self.cardString]){
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view
                                                        WithType:NavTipsViewTypeMessage
                                                            text:@"请输入有效的身份证号码"
                                                      isExistNav:YES];
        return;
    }
    
    
    //传递信息
    [[SYProgressHUD sharedInstance] showMBHudWithSting:kLoadingText];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.nameString forKey:@"name"];
    [dict setObject:self.cardString forKey:@"idCard"];
    
    [BaseNetRequest sessionRequestWithparameters:dict method:POSTMethod requstType:PostRealName WithSuccessBlock:^(NSDictionary *resultDict) {
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        DDLog(@"resultDict = %@",resultDict);
        
        if ([resultDict[@"code"] integerValue] == 200) {//认证成功
            DDLog(@"认证成功")
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            [[NavTipsView defaultTipsView] showTipsBannerInView:self.view
                                                            WithType:NavTipsViewTypeMessage
                                                                text:resultDict[@"message"]
                                                          isExistNav:YES];
        }
    } failBlock:^(NSError *error) {
        DDLog(@"%@",error)
        [[SYProgressHUD sharedInstance] dismissMBWithTime:0];
        [[NavTipsView defaultTipsView] showTipsBannerInView:self.view
                                                        WithType:NavTipsViewTypeMessage
                                                            text:@"认证出错，请稍后重试~"
                                                      isExistNav:YES];
    }];
    
    
    
    
}


@end
